home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4559 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.2 KB  |  77 lines

  1. Path: dur-news.ctron.com!dur-news!grieve
  2. From: grieve@ATM-ws42 (David Grieve)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Two way communication between objects
  5. Date: 30 Jan 1996 20:52:09 GMT
  6. Organization: Cabletron Systems, Inc.
  7. Distribution: world
  8. Message-ID: <GRIEVE.96Jan30155209@ATM-ws42>
  9. References: <310ACCE2.2600@werple.mira.net.au>
  10.     <ENNO.96Jan28110653@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  11. NNTP-Posting-Host: atm-ws42.ctron.com
  12. In-reply-to: enno@inferenzsysteme.informatik.th-darmstadt.de's message of 28 Jan 1996 10:06:52 GMT
  13.  
  14. In article <310ACCE2.2600@werple.mira.net.au> Ross Forder <erosco@werple.mira.net.au> writes:
  15.  
  16.    I have to apologise what what is probably a stupid question but I'm only
  17.    a beginner at some of this stuff.
  18.  
  19.    I am trying to write a VERY simple client and server set of objects. I 
  20.    would like to client to register with the server at ctor time and have 
  21.    the server know about the client by saving a pointer to the client 
  22.    so he can callback to a method called say 'event'. 
  23.  
  24.    The problem is the fact that they will both need a pointer to each other 
  25.    and this seems impossible using strong typing. Is there a simple 'object 
  26.    oriented' way to do this?
  27.  
  28.    I have been able to make this work by having the server only know about 
  29.    a parent class of the client (say eventhandler) but this is still not a 
  30.    bulletproof solution.
  31.  
  32.    Can someone set me straight?
  33.  
  34. Maybe I don't understand the problem. How about something like this
  35. (although the objects are strongly coupled). This could easily be
  36. extended to a n:1 client-server relationship.
  37.  
  38. class Client;
  39.  
  40. class Server;
  41. {
  42. private:
  43.         Client* client;
  44. public:
  45.         Server();
  46.         void register_client(Client* clnt) { client = clnt; }
  47.         void notify_client() { client->event(); }
  48. };
  49.  
  50. class Client
  51. {
  52. private:
  53.         Server* server;
  54. public:
  55.         Client(Server* srvr)
  56.         {
  57.                  server = srvr;
  58.                  server->register_client(this); 
  59.         }
  60.  
  61.         void event() { cout << "foobar!"; }
  62. };
  63.  
  64. main()
  65. {
  66.         Server s;
  67.         Client c(&s);
  68.  
  69.         s.notify_client();
  70. }
  71.  
  72.                     
  73. --
  74. David Grieve            | grieve@ctron.com
  75. Cabletron Systems, Inc. | 603-337-7622
  76. Durham, NH USA 03824    | Opinions expressed are my wife's.
  77.